package com.android.base.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;

import com.example.lib_base.R;
import com.example.lib_base.databinding.LibBaseDialogLoadingBinding;


/**
 * FileName: DialogLoading
 * Author: admin
 * Date: 2020/5/21 14:28
 * Description:
 */
public class DialogLoading extends Dialog {

    private Activity activity;
    private LibBaseDialogLoadingBinding loadingBinding = null;
    private String content;
    private int duration = 0;

    public DialogLoading(@NonNull Activity context) {
        super(context, R.style.dialog_loading);
        this.activity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        loadingBinding = LibBaseDialogLoadingBinding.inflate(LayoutInflater.from(activity));
        setContentView(loadingBinding.getRoot());
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getScreenWidth(activity) / 4;
        lp.height = getScreenWidth(activity) / 4;
        getWindow().setAttributes(lp);

        if (TextUtils.isEmpty(content)) {
            loadingBinding.tvContent.setText("加载中...");
        } else {
            loadingBinding.tvContent.setText(content);
        }

        Animation rotateAnimation = AnimationUtils.loadAnimation(activity, R.anim.lib_base_anim_loading);
        LinearInterpolator lin = new LinearInterpolator();
        rotateAnimation.setInterpolator(lin);
        loadingBinding.imgIcon.startAnimation(rotateAnimation);

        if (duration != 0) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dismiss();
                }
            }, duration);
        }
    }

    public DialogLoading setContent(String c) {
        this.content = c;
        return this;
    }

    public DialogLoading setDuration(int d) {
        this.duration = d;
        return this;
    }

    /**
     * 获取屏幕的宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void dismiss() {
        loadingBinding = null;
        super.dismiss();
    }
}